package com.xxx.video.mine.presenters;

import android.content.Context;

import androidx.annotation.NonNull;

import com.xxx.video.commen.beans.BaseBean;
import com.xxx.video.commen.presenters.BasePresenter;
import com.xxx.video.commen.utils.ResponseUtils;
import com.xxx.video.login.beans.UserInfoBean;
import com.xxx.video.mine.beans.UploadFileBean;
import com.xxx.video.network.listeners.CommonResponseListener;
import com.xxx.video.network.utils.NetWorkUtils;

import java.io.File;

public class AccountInformationPresenter extends BasePresenter<IAccountInformationView> {

    public AccountInformationPresenter(@NonNull Context context, IAccountInformationView baseView) {
        super(context, baseView);
    }

    /**
     * get user info
     */
    public void getUserInfo() {
        NetWorkUtils.getInstance().getUser(new CommonResponseListener<UserInfoBean>() {
            @Override
            public void onSuccess(UserInfoBean successOb, String result) {
                if (getBaseView() != null) {
                    getBaseView().showRootLayoutStatus(1);
                }
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().initUserInfoData2View(successOb);
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {
                if (getBaseView() != null) {
                    getBaseView().showRootLayoutStatus(1);
                }
            }
        });
    }

    /**
     * upload file
     *
     * @param file file
     */
    public void uploadFile(File file) {
        showProgressDialog("loading");
        NetWorkUtils.getInstance().uploadFile(file, new CommonResponseListener<UploadFileBean>() {
            @Override
            public void onSuccess(UploadFileBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    updateUserInfo(successOb.url, "", "");
                } else {
                    hideProgressDialog();
                }
            }

            @Override
            public void onFailure(Object failureObj) {
                hideProgressDialog();
            }
        });
    }

    public void updateUserInfo(String avatarUrl, String gender, String age) {
        NetWorkUtils.getInstance().updateInfo("", "", "", avatarUrl, gender, age, new CommonResponseListener<BaseBean>() {

            @Override
            public void onSuccess(BaseBean successOb, String result) {
                hideProgressDialog();
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().upDateAvatarInfoSuccess(avatarUrl);
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {
                hideProgressDialog();

            }
        });
    }

}
